import getDataForDB from '@/api/qsbserviceEnterprise'
import Storage_ from './LocalStorage'

/**
 *
 * @param { String | Array} listenerKeys  监听的keys 当listenerKeys的类型为字符串时
 * @param {Function} callBack 回调函数 callBack(key,val)
 */
const LocalStorageChange = (listenerKeys, callBack) => {
  function newSetItemEvent(e) {
    // doSomething
    const key = e.newKey
    const val = e.newValue
    if (Object.prototype.toString.call(listenerKeys) === '[object String]' && listenerKeys == key) {
      callBack && callBack(key, val)
    }
    if (Object.prototype.toString.call(listenerKeys) === '[object Array]' && listenerKeys.indexOf(key) >= 0) {
      callBack && callBack(key, val)
    }
  }
  // 需要每次执行之前先清空指定事件
  window.removeEventListener('newSetItemEvent', newSetItemEvent);
  // 监听抛出的自定义事件
  window.addEventListener("newSetItemEvent", newSetItemEvent);

  var orignSetItem = window.localStorage.setItem;
  window.localStorage.setItem = function (key, newValue) {
    var newSetItemEvent = new Event("newSetItemEvent");
    newSetItemEvent.newValue = newValue;
    newSetItemEvent.newKey = key;
    window.dispatchEvent(newSetItemEvent); // 抛出自定义事件
    orignSetItem.apply(this, [key, newValue]);
  }
}



const BusinessType = {
  999: "充值",
  1000: "提现",
  1001: "企业注册",
  1002: "企业服务续费",
  1003: "开票",
  1004: "退票手续费",
  1005: "银行开户",
  1006: "工商注册信息变更",
  1007: "企业注销",
  1008: "税务变更",
  1009: "电子签章",
  1013: "税务登记",
  1014: "税务申报（核定）",
  1015: "税务申报（查账）",
  1016: "企业服务模式变更",
  1017: "邮寄资料",
  1018: "退票服务",
  1120: "增值业务"

  // 1001: '企业注册',
  // 1002: '服务年费',
  // 1003: '开票服务费',
  // 1004: '退票服务费',
  // 1005: '银行开户',
  // 1006: '注册信息变更',
  // 1007: '企业注销',
  // 1008: '税务变更',
  // 1009: '电子签章',
  // 1013: '税务登记',
  // 1014: '税务申报',
  // 1015: '个税申报',
  // 1016: '企业服务模式变更',
  // 1017: '邮寄资料',
}
/**
 * 检查用户是否登陆
 * @returns
 */
const checkUser = () => {
  // // pro__Access-Token
  // const token = localStorage.getItem('pro__Access-Token')
  // // console.log('checkUser 0000000000000=====>', token);
  // if (token) {
  //   return true
  // }
  return true
}

/**
 * 获取 当前系统 业务类型
 * @returns
 */
export const getBusinessType = () => {
  const tempData = { optionList: [], tableTypeKeyName: {}, tableTypeKeyValue: {} }
  // { label: '税务登记', value: '1013' },
  // 1001: '注册服务费',
  // 注册服务费: '1001',
  if (!checkUser()) {
    return false
  }
  getDataForDB('获取所有BusinessType数据').then(res => {
    const { code, result } = res
    if (code == 200) {
      result.forEach(item => {
        const { name, value } = item
        tempData.optionList.push({ label: name, value: value })
        tempData.tableTypeKeyName[value] = name
        tempData.tableTypeKeyValue[name] = value
      })
    }
    Storage_.set('BusinessType', tempData)
    // console.log('获取所有BusinessType数据 ===>', tempData);
  })

}

export const getSubitem = () => {
  const tempData = { optionList: [], tableTypeKeyName: {}, tableTypeKeyValue: {} }
  // { label: '税务登记', value: '1013' },
  // 1001: '注册服务费',
  // 注册服务费: '1001',
  if (!checkUser()) {
    return false
  }
  getDataForDB('获取所有Subitem数据').then(res => {
    const { code, result } = res
    if (code == 200) {
      result.forEach(item => {
        const { name, value } = item
        tempData.optionList.push({ label: name, value: value })
        tempData.tableTypeKeyName[value] = name
        tempData.tableTypeKeyValue[name] = value
      })
    }
    Storage_.set('SubitemType', tempData)
    // console.log('获取所有BusinessType数据 ===>', tempData);
  })

}


export default () => {
  history.pushState = new Proxy(history.pushState, {
    apply: function (target, thisBinding, args) {
      console.log('sssssssssssssssssssssssssssssssss',target,args,history.pushState);
      // if(args.join('').indexOf('login')<0){
      //   getBusinessType()
      //   getSubitem()
      // }
      getBusinessType()
      getSubitem()
      return target.apply(thisBinding, args);
    },
  });


}
